<?php

namespace app\store\model;


class UserScoreLogModel extends SearchLogModel
{

    protected $connection = 'db_platform_operatelog';

    protected $table = 'game_burying_point';

    // 破产的条件 自己独有的
    protected $condition = [
        ['point_ID', '=', 52],
        ['SpreaderID', '=', 888],
    ];

    /**
     * UserScoreLogModel constructor.
     * @param array $data
     * @throws \think\exception\DbException
     */
    public function __construct($data = [])
    {
        parent::__construct($data);

        $this->trade_sub_sql = TradeModel::getUserIdsSubSql($this->date_range);

    }


    /**
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search()
    {

        if ($this->time_field == 'RegisterDate') {
            // 查询出当天的记录id
            $row = self::fieldRaw('ID,new_count as count,add_Number as number')
                ->where($this->condition)
                ->where('date', 'between', $this->date_range)
                ->find();
        } else {
            // 查询出当天的记录id
            $row = self::fieldRaw('ID,active_count as count,Number as number')
                ->where($this->condition)
                ->where('date', 'between', $this->date_range)
                ->find();
        }

        if(empty($row)){
            return;
        }

        // 如果用户搜索条件为空 就直接返回
        if (empty($this->where)) {
            // 查询充值情况 通过uid查询并且当天充值过的用户
            $res = self::table('game_burying_point_user u')
                ->fieldRaw('count(*) as number,sum(count) as count')
                ->where('P_ID', '=', $row['ID'])
                ->where('u.UserID in ' . $this->trade_sub_sql)
                ->find();
        } else {

            $query = self::table('game_burying_point_user u')
                ->fieldRaw('count(*) as number,sum(u.count) as count')
                ->where('P_ID', '=', $row['ID'])
                ->where($this->where);
            if ($this->time_field == 'RegisterDate') {
                $query->where('u.type_ID','=',1);
            }

            $query1 = clone $query;

            if (array_key_exists('g.Cannon', $this->where) or
                (isset($this->where[0][0]) and $this->where[0][0] == 'g.Cannon')) {
                // 如果筛选炮倍

                $row = $query
                    ->leftJoin('qpaccountsdb.user_game_info g', 'u.UserID = g.UserID')
                    ->find();
                // echo $query1->getLastSql();

                $res = $query1
                    ->where('u.UserID in ' . $this->trade_sub_sql)
                    ->leftJoin('qpaccountsdb.user_game_info g', 'u.UserID = g.UserID')
                    ->find();

            } else {
                // 如果筛选其他用户条件
                $row = $query
                    ->leftJoin('qpaccountsdb.accounts_info a', 'u.UserID = a.UserID')
                    ->find();

                $res = $query1
                    ->where('u.UserID in ' . $this->trade_sub_sql)
                    ->leftJoin('qpaccountsdb.accounts_info a', 'u.UserID = a.UserID')
                    ->find();
            }
        }

        $arr = [
            'all' => [
                'times' => $row['count'],
                'number' => $row['number'],
                'avg_times' => $row['number'] ? round($row['count'] / $row['number'], 1) : 0
            ],
            'pay' => [
                'number' => $res['count'],
                'times' => $res['number'],
                'avg_times' => $res['number'] ? round($res['count'] / $res['number'], 1) : 0
            ]
        ];

        return $arr;
    }

    protected function getDbQuery()
    {
        // TODO: Implement getDbQuery() method.
    }

}
